package com.nufront.bloodsugar.view;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class LineChart extends LinearLayout {

	private Context mContext;
	private int XValue = 5;

	private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
	private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
	private GraphicalView chartView;
	private XYSeries mCurrentSeries = new XYSeries("");
	private XYSeriesRenderer mCurrentRenderer = new XYSeriesRenderer();;

	private int[] xV = new int[20];
	private double[] yV = new double[20];

	public LineChart(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mContext = context;

		init();
		this.addView(chartView);
	}

	public LineChart(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	private void init() {
		int colorPoint = Color.rgb(171, 76, 30);// #ab4c1e
		int colorLine = Color.rgb(66, 45, 13);// #422d0d

		mRenderer.addSeriesRenderer(mCurrentRenderer);
		mDataset.addSeries(mCurrentSeries);

		mRenderer.setXAxisMin(0);
		mRenderer.setXAxisMax(100);
		mRenderer.setYAxisMin(0);
		mRenderer.setYAxisMax(35);
		mRenderer.setShowAxes(false);
		mRenderer.setShowGrid(false);
		mRenderer.setShowLabels(false);
		mRenderer.setShowLegend(false);
		mRenderer.setZoomButtonsVisible(false);
		mRenderer.setYLabelsAlign(Align.RIGHT);
		// mRenderer.setPanLimits(new double[] { -100, 150, -10, 50 });
		// mRenderer.setZoomLimits(new double[] { -10, 20, -10, 20 });
		mRenderer.setApplyBackgroundColor(true);
		mRenderer.setBackgroundColor(Color.TRANSPARENT);
		mRenderer.setMargins(new int[] { 0, 0, 0, 0 });
		mRenderer.setXLabels(10);
		mRenderer.setPointSize(7);

		mCurrentRenderer.setPointStyle(PointStyle.CIRCLE);
		mCurrentRenderer.setColor(colorPoint);
		mCurrentRenderer.setFillPoints(true);
		mCurrentRenderer.setLineWidth(3);
		mCurrentRenderer.setLineColor(colorLine);
		mCurrentRenderer.setDisplayChartValues(true);
		mCurrentRenderer.setChartValuesTextSize(20);
		mCurrentRenderer.setChartValuesSpacing(10f);
		mCurrentRenderer.setChartValuesTextColor(colorLine);

		if (chartView == null) {
			chartView = ChartFactory.getLineChartView(mContext, mDataset,
					mRenderer);
			mRenderer.setClickEnabled(true);
			mRenderer.setSelectableBuffer(100);
		} else {
			chartView.repaint();
		}
	}

	public void addSeries(double yValue) {
		if (chartView != null) {
			if (mCurrentSeries.getItemCount() < 19) {
				mCurrentSeries.add(XValue, yValue);
				chartView.repaint();
			} else {
				updateChart(yValue);
			}
		}

		XValue += 5;
	}

	private void updateChart(double yValue) {
		mDataset.removeSeries(mCurrentSeries);// 移除Dataset中旧的数据点

		int length = mCurrentSeries.getItemCount();


		if (length > 20) {
			length = 20;
		}

		for (int i = 0; i < length; i++) {
			xV[i] = (int) (mCurrentSeries.getX(i) - 5);
			yV[i] = mCurrentSeries.getY(i);
		}

		mCurrentSeries.clear();

		for (int i = 1; i < length; i++) {
			mCurrentSeries.add(xV[i], yV[i]);
		}
		mCurrentSeries.add(95, yValue);
		mDataset.addSeries(mCurrentSeries);

		chartView.repaint();
	}

}
